package com.ilop.siteraudio.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.ilop.siteraudio.R;
import com.ilop.siteraudio.util.StatusBarUtil;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2/6/21.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class FAQActivity extends AppCompatActivity {

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.web_view)
    WebView mWebView;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.iv_faq_back)
    ImageView mFAQBack;

    private String mScript;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_faq);
        StatusBarUtil.setDefaultStatus(this);
        ButterKnife.bind(this);

        int mFAQType = getIntent().getIntExtra("faq_type", 0);

        mFAQBack.setOnClickListener(view -> finish());

        mWebView.getSettings().setJavaScriptEnabled(true);//启用js

        mWebView.getSettings().setBlockNetworkImage(false);//解决图片不显

        mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

        mWebView.loadUrl(getFAQUrlPath());

        mScript = "javascript:setShowTips" + "(" + mFAQType + ")";

        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mWebView.evaluateJavascript(mScript, s -> {});
            }
        });
    }


    private String getFAQUrlPath(){
        Locale locale = getResources().getConfiguration().locale;
        String lan = locale.getLanguage();
        if ("zh".equals(lan)) {
            return "file:///android_asset/register_zh.html";
        }
        return "file:///android_asset/register_en.html";
    }
}
